var myPrefix = jsctx + "/financialStatement";

var financialStatement = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            loadChart(0);
        }, 10)
    };

    function openLoading(bizType) {
        $("#loadBox"+bizType).show();
        $("#container"+bizType).hide();
    }

    function closeLoading(bizType) {
        $("#loadBox"+bizType).hide();
        $("#container"+bizType).show();
    }

    function loadChart(bizType) {
        openLoading(bizType);
        var provinceId = '';
        var cityId = '';
        var areaId = '';
        var stationName = '';
        if(bizType==0){
            provinceId = $("#provinceId").val();
            cityId = $("#cityId").val();
            areaId = $("#areaId").val();
            stationName = $("#stationName").val();
        }else{
            stationName = $("#stationName2").val();
        }
        $.ajax({
            url: myPrefix + "/data?provinceId="+provinceId+"&cityId="+cityId+"&areaId="+areaId+"&stationName="+stationName+"&bizType="+bizType,
            type: "GET",
            success: function (data) {
                closeLoading(bizType);
                if (data.status == 200) {
                    var seriesData = [];
                    var objData = data.data;
                    // SELECT COUNT(1) AS orderTotal, SUM(o.total_power) AS powerTotal, SUM(o.pay_amount) AS amountTotal
                    /*data: [
                        ['订单量', 320],
                        ['充电量', 380],
                        ['累计收入', 350]
                    ]*/
                    if(objData){
                        var orderTotal = objData.orderTotal || 0;
                        var powerTotal = objData.powerTotal || 0;
                        var amountTotal = objData.amountTotal || 0;
                        var orderInfo = ['订单量', orderTotal];
                        var powerInfo = ['充电量', powerTotal];
                        var amountInfo = ['累计收入', amountTotal];
                        seriesData.push(orderInfo);
                        seriesData.push(powerInfo);
                        seriesData.push(amountInfo);
                    }
                    ShowChart(bizType,seriesData);
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }

    function ShowChart(bizType,seriesData) {
        var chart = Highcharts.chart('container'+bizType, {
            chart: {
                type: 'column'
            },
            title: {
                text: ''
            },
            credits: {
                text: '',
            },
            xAxis: {
                type: 'category'
            },
            yAxis: {
                min: 0,
                title: {
                    text: ''
                }
            },
            legend: {
                enabled: false
            },
            tooltip: {
                pointFormat: '<b>{point.y:.2f} </b>'
            },
            series: [{
                name: '',
                data: seriesData
                /*data: [
                    ['订单量', 320],
                    ['充电量', 380],
                    ['累计收入', 350]
                ]*/
            }]
        });
    }

    function reset() {
        $("#provinceId").val("");
        $("#cityId").val("");
        $("#areaId").val("");
        $("#stationName").val("");
        $("#stationName2").val("");
    }

    function goBack() {
        location.href = myPrefix + "/index";
    }

    /**
     * 导出文件
     */
    function exportFile(bizType) {
        var area = '';
        var stationName = '';
        if(bizType==0){
            area = $("#area").val();
            stationName = $("#stationName").val();
        }else{
            stationName = $("#stationName2").val();
        }
        $.fileDownload(myPrefix + "/export", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                area: area.trim(),
                stationName: stationName.trim(),
                bizType: bizType
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    return {
        init: init,
        goBack: goBack,
        reset: reset,
        loadChart: loadChart,
        exportFile: exportFile
    };
})();
